<template>
  <div id="cosumer-handler">
    <el-form :model="consumer" label-width="120px" class="form" :rules="formRules" ref="form">
      <el-form-item label="消费者名称" prop="consumerName">
        <el-input v-model="consumer.consumerName" placeholder="请输入消费者名称" class="g-input-width"></el-input>
      </el-form-item>
      <el-form-item label="消费者编码" prop="consumerNameCode">
        <el-input v-model="consumer.consumerNameCode" placeholder="请输入消费者编码" class="g-input-width"></el-input>
      </el-form-item>
      <el-form-item label="调用次数" prop="invoke">
        <el-input-number v-model="consumer.invoke" placeholder="请输入调用次数"></el-input-number>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-radio-group v-model="consumer.status">
          <el-radio :label="0">草稿</el-radio>
          <el-radio :label="1">已发布</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="是否启用" prop="isEnable">
        <el-switch v-model="consumer.isEnable"></el-switch>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
const consumer = {
  consumerName: '',
  consumerNameCode: '',
  invoke: 0,
  status: 0,
  isEnable: '0',
}
export default {
  data() {
    return {
      submitMode: 'add',
      consumer,
      formRules: {
        consumerName: [
          { required: true, trigger: 'blur', message: '请输入消费者名称' }
        ],
        consumerNameCode: [
          { required: true, trigger: 'blur', message: '请输入消费者编码' },
          { pattern: /^[a-zA-Z_-]*$/, trigger: 'blur', message: '消费者编码必须是英文' },
        ]
      }
    }
  },
  methods: {
    init(submitMode, row) {
      this.reset()
      this.submitMode = submitMode
      submitMode == 'edit' && ( this.consumer = row )
    },
    reset() {
      this.consumer = { ...consumer }
      this.$refs.form.clearValidate()
    },
    handleSubmit() {
      this.$refs.form.validate( valid => {
        if(valid) {
          console.log(this.consumer)
          let message = this.submitMode == 'add'?message = '新增':message = '修改'
          this.$message.success(message+'成功')
          this.$emit('load')
        } else {
          this.$message.warning('请检查表单项')
          return false
        }
      })
    }
  }
}
</script>
<style lang="scss" scoped>
.g-input-width {
  width: 100%;
}
</style>